/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package overlaynetwork;


import java.net.*;
import java.util.*;
import connection.Connection;
import protocol.*;
import java.util.concurrent.LinkedBlockingQueue;


public class OverlayNetwork {

    private Connection connection;
    private InetSocketAddress address;
    private LinkedBlockingQueue<Packet> qPackets;
    private Hashtable<String, Packet> cachePackets;
    private Hashtable<Integer, Peer> peers;
    private int myID;
    private Protocol protocol;
    private QPacketsController qPacketsController;

    public OverlayNetwork(){
        
        connection = new Connection(this);
        address = connection.getInetSocketAddress();
        peers = new Hashtable<Integer, Peer>();
        qPackets = new LinkedBlockingQueue<Packet>();
        qPacketsController = new QPacketsController(this);
        cachePackets = new Hashtable<String, Packet>();

        protocol = new Protocol(connection);
        try{
            Thread.currentThread().sleep(5000);
        }catch(Exception e){

        }
       new createOverlay(this);
       new QPacketsController(this).start();
       new PingController(this).start();

        }


    public Hashtable<Integer, Peer> getPeers(){
        return peers;
    }

    public int getID(){
        return myID;
    }

    public void setID(int id){
        myID = id;
    }

    public InetSocketAddress getInetSocketAddress(){
        return address;
    }

    public Connection connect(){
        return connection;
    }

    public Protocol getProtocol(){
        return protocol;
    }

    public LinkedBlockingQueue<Packet> getQPackets(){
        return qPackets;
    }

    public Hashtable<String, Packet> getCachePackets(){
        return cachePackets;
    }


    public Peer getPeer(InetSocketAddress address){

        for(int i=0; i<peers.size(); i++){
            Peer peer = peers.get(i);
            if(peer.getInetSocketAddress().equals(address))
                return peer ;
        }
        return null;

    }

}
